package atempdao;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import util.HibernateUtil;
import dao.EgnMarketVari;
import dao.EgnRstScenario;
import dao.EgnRstScenarioId;

public class EgnRstScenarioDao {
	private final static Logger logger = LoggerFactory.getLogger(EgnRstScenarioDao.class);

	public static List<EgnRstScenario> getAllEgnRstScenario() {
		List<EgnRstScenario> rst ;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get all EgnRstScenario instance 00 ");
		Query qr = s.createQuery("from EgnRstScenario" );

		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}
	public static List<EgnRstScenarioId> getEgnRstScenarioId(String sceSetId) {
		List<EgnRstScenarioId> rst ;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get all EgnRstScenario instance 11");
		
		Query qr = s.createQuery(" select distinct new dao.EgnRstScenarioId( a.id.sceId,a.id.sceId, a.id.sceId)" +
								 " from EgnRstScenario a where a.sceSetId =:param" 
					);
		qr.setParameter("param", sceSetId);
		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
		 
	}

	public static List<EgnRstScenario> getEgnRstScenario(String sceSetId) {
		List<EgnRstScenario> rst ;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get all EgnRstScenario instance 11");
		
		Query qr = s.createQuery("from EgnRstScenario a where a.sceSetId =:param" );
		qr.setParameter("param", sceSetId);
		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}
	
//	public static List<String> getEgnRstScenaioId(String sceSetId) {
//		List<String> rst ;
//		Session s = HibernateUtil.currentSession();
//		logger.debug("Get all EgnRstScenario instance 22");
//		Query qr = s.createQuery(" select distinct a.id.sceId from EgnRstScenario a " +
//								 " where a.sceSetId =:param" );
//		qr.setParameter("param", sceSetId);
//		try {
//			rst = qr.list();
//			HibernateUtil.closeSession();
//			return rst;
//		} catch (RuntimeException re) {
//			logger.error("failed", re);
//			HibernateUtil.closeSession();
//			throw re;
//		}
//	}
	
	public static List<String> getEgnRstScenaioSetId() {
		List<String> rst ;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get all EgnRstScenario instance 33");
		Query qr = s.createQuery("select distinct a.sceSetId from EgnRstScenario a" );

		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}
	public static String getEgnRstScenaioSetId(EgnRstScenarioId sceId) {
		String rst ;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get all EgnRstScenarioSetId instance 33");
		Query qr = s.createQuery("select distinct a.sceSetId from EgnRstScenario a where a.id.sceId =:param" );
		qr.setParameter("param", sceId.getSceId());
		try {
			rst = (String)qr.uniqueResult();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}
	
	public static List getEgnMarketVariScenario(EgnRstScenarioId sceId) {
		List rst = new ArrayList();
		Session s = HibernateUtil.currentSession();
		logger.debug("Get all EgnRstScenarioSetId instance 33");
		Query qr = s.createQuery(
				" select b.id.baseDate,b.id.mvId ,b.id.maturityId,b.mvValue, a.coefficient, a.power, a.spread" +
				" from EgnRstScenario a, EgnMarketVariHis b" +
				" where a.id.mvId = b.id.mvId" +
				" and a.id.maturityId = b.id.maturityId" +
				" and b.id.baseDate = :param" +
				" and a.id.sceId = :param2 "
				);
		qr.setParameter("param", EgnMarketVariDao.getEgnMarketVairHisLastDate());
		qr.setParameter("param2", sceId.getSceId());
		try {
			rst = qr.list();
//			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
//			HibernateUtil.closeSession();
			throw re;
		}
	}
	
	

}
